Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Custom error messages with validation message tag helper #8035

Closed
coxp opened this issue Jul 9, 2018 · 6 comments
Closed

Custom error messages with validation message tag helper #8035

coxp opened this issue Jul 9, 2018 · 6 comments
Assignees
Labels
3 - Done bug cost: S Will take up to 2 days to complete PRI: 2 - Preferred Preferably should be handled during the milestone.

Comments

@coxp
Copy link

coxp commented Jul 9, 2018

Is this a Bug or Feature request?:

Feature request or documentation request

Steps to reproduce (preferably a link to a GitHub repo with a repro project):

Add a simple model and a view that uses the validation message tag helper.

public class Entity
{
    [Required]
    public string Name { get; set; }
}

<span asp-validation-for="Name">Custom error message</span>

Validation message is displayed as "The Name field is required" rather than "Custom error message"

Description of the problem:

Simple workaround is to add the data-valmsg-replace="false" along with the asp-validation-for attribute.

<span asp-validation-for="Name" data-valmsg-replace="false">Custom error message</span>

The ValidationMessageTagHelper does check for inner content but the data-valmsg-replace attribute has already been set to true at this point as a null message parameter is passed to the HtmlGenerator. Ideally the data-valmsg-replace would be set to false at this point if the element has inner content. Alternatively change the validation message documentation to show how to set the data-valmsg-replace attribute for custom error messages.

Version of Microsoft.AspNetCore.Mvc or Microsoft.AspNetCore.App or Microsoft.AspNetCore.All:

2.1.1

@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @coxp.
@dougbu, looks like this is a pure documentation related. Though I'm not sure we don't have this documented properly. If not, let's use this issue to fix/add documentation.

@dougbu
Copy link
Member

dougbu commented Jul 9, 2018

@mkArtakMSFT since the current behaviour is counter-intuitive, suggest we update ValidationMessageTagHelper to detect existing content before calling into the IHtmlGenerator. That is, we should treat overwriting the explicit <span> content as a bug.

@dougbu dougbu removed the investigate label Jul 9, 2018
@mkArtakMSFT
Copy link
Member

Sounds good, @dougbu.
To avoid breaking existing users, we should use a compatibility switch to opt into the proposed behavior.

@mkArtakMSFT mkArtakMSFT added bug 1 - Ready PRI: 2 - Preferred Preferably should be handled during the milestone. labels Jul 10, 2018
@mkArtakMSFT mkArtakMSFT added this to the 2.2.0-preview1 milestone Jul 10, 2018
@dougbu dougbu added the cost: S Will take up to 2 days to complete label Jul 15, 2018
@dougbu
Copy link
Member

dougbu commented Jul 15, 2018

Thanks to @kishanAnem for PR #8087. He's well on the way to fixing this issue.

@kishanAnem
Copy link
Contributor

kishanAnem commented Jul 16, 2018

@dougbu welcome. :) . I'm glad to work with you.

kishanAnem pushed a commit to kishanAnem/Mvc that referenced this issue Jul 21, 2018
dougbu added a commit that referenced this issue Jul 22, 2018
Custom error messages with validation message tag helper #8035
@dougbu
Copy link
Member

dougbu commented Jul 22, 2018

ff4e2eb

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
3 - Done bug cost: S Will take up to 2 days to complete PRI: 2 - Preferred Preferably should be handled during the milestone.
Projects
None yet
Development

No branches or pull requests

4 participants